package com.franklin.ideaplugin.api.ui.editor;

import com.franklin.ideaplugin.api.icon.ApiIcons;
import com.franklin.ideaplugin.api.ui.IBaseComponent;
import com.franklin.ideaplugin.api.utils.ProjectUtils;
import com.franklin.ideaplugin.easytesting.icon.PluginIcons;
import com.franklin.ideaplugin.easytesting.ui.Sizes;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.ui.popup.*;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
 * @author Ye Junhui
 * @since 2023/7/12
 */
public interface IBaseZoomInEditor extends IBaseComponent {

    /**
     * 初始化Format按钮
     * @param customEditor
     * @param zoomInBtn
     */
    default void initZoomInButton(CustomEditor customEditor, JButton zoomInBtn,@Nullable String title){
        zoomInBtn.setIcon(ApiIcons.SEARCH_WITH_HISTORY_ICON);
        zoomInBtn.setText("Enlarge the text editor");
        zoomInBtn.setToolTipText("Enlarge the text editor");
        zoomInBtn.addActionListener(e -> {
            JBPopupFactory jbPopupFactory = JBPopupFactory.getInstance();
            CustomEditor newEditor = new CustomEditor(customEditor.getProject(), customEditor.getFileType());
            newEditor.setText(customEditor.getText());
            newEditor.setPsiType(customEditor.getPsiType());

            ConfirmCustomJsonEditor confirmCustomerEditor = new ConfirmCustomJsonEditor(newEditor);

            ComponentPopupBuilder componentPopupBuilder = jbPopupFactory.createComponentPopupBuilder(confirmCustomerEditor.getMainPanel(), confirmCustomerEditor.getMainPanel());
            JBPopup zoomInPopup = componentPopupBuilder
                    .setTitle(title)
                    .setCancelKeyEnabled(true)
                    .setCancelButton(new IconButton("close", AllIcons.Actions.CloseHovered))
                    .setTitleIcon(new ActiveIcon(PluginIcons.PLUGIN_ICON))
                    .setMovable(true)
                    .setShowShadow(true)
                    .setResizable(true)
                    .setRequestFocus(true)
                    .setCancelOnClickOutside(false)
                    .setCancelOnMouseOutCallback(mouseEvent -> false)
                    .setCancelOnOtherWindowOpen(false)
                    .setCancelOnWindowDeactivation(false)
                    .createPopup();

            zoomInPopup.setSize(Sizes.ExecuteMethodFormSize.ZOOM_IN_SIZE);
            zoomInPopup.showCenteredInCurrentWindow(ProjectUtils.getCurrProject());
            confirmCustomerEditor.initConfirmButton(confirmBtn -> {

                confirmBtn.addActionListener(e1 -> {
                    customEditor.setText(confirmCustomerEditor.getEditor().getText());
                    zoomInPopup.cancel();
                });
            });
        });
    }
}
